import tableInfo from "../components/tableInfo.vue"
import {
	SqlBuilder
} from './SqlBuilder'
import {
	metadataCovert,
	metaDefaultConfg
} from "./constant"
import {ScriptApi} from "../api/scriptApi"
class ResouceTableType {
	constructor(opt, params) {
		this.opt = opt;
		this.params = params
		this.node = params.node;
		this.messageBox = opt.messageBox;
		this.sqlBuilder = new SqlBuilder();
		this.scriptApi = new ScriptApi(opt.request,opt.message)
	}

	getMetadata() {
		const {
			connId,
			catalog,
			tableName
		} = this.node.data.properties
		const rowCovert = (d, row) => {

		  d.name = row.TABLE_NAME;
		  d.id = `${connId}_${catalog}_${d.name}`;
		  d.type = 'TABLE';
		  d.properties.tableName = d.name;
		  d.leaf = true;
	
		}
		return new Promise((resolve, reject) => {
		  const executeParams = {
			script: this.sqlBuilder.selectTables(catalog),
			noPage:true
		  };
		  const params = {
			connId: this.node.data.properties.connId,
			executeParams: executeParams
		  };
		  this.scriptApi.singleExecute(params).then(res => {
			resolve(metadataCovert(this.opt, rowCovert, res, connId,catalog,tableName))
		  }).catch(e => {
			reject(e)
		  })
		})
	  }
	

	getMetadataConfig() {
		return {
			buildScript: () => {
				const {
					connId,
					catalog,
					tableName
				} = this.node.data.properties
				return this.sqlBuilder.selectTables(catalog)
			},
			rowCovert: (d, row) => {
				const {
					connId,
					catalog,
					tableName
				} = this.node.data.properties
				d.name = row.TABLE_NAME;
				d.id = `${connId}_${catalog}_${d.name}`;
				d.type = 'TABLE';
				d.properties.tableName = d.name;
				d.leaf = true;
			},
			covert: (res) => {
				return metadataCovert(this.opt, this.getMetadataConfig().rowCovert, res, this.node)
			}
		}
	}


	getContextMenus() {
		let menus = [];

		menus.push({
			label: "新建表",
			dividedTop: true,
			click: (event, item) => {
				const {
					connId,
					catalog,
					puginId
				} = this.node.data.properties
				const params = {
					connId,
					catalog,
					puginId
				}
				this.opt.bus.$emit(this.opt.config.CONTEXT_MENU_EVENT_KEY, {
					id: `${this.node.id}_design`,
					type: this.opt.config.CONTEXT_MENU_TYPE_TAGS_COM,
					title: `${catalog}@未定义-表`,
					data: params,
					component: tableInfo
				})
				return true;
			},
		})



		return menus
	}
}

export {
	ResouceTableType
}
